package com.dreamcode.caloriestracker.collectors;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.dreamcode.caloriestracker.database.DbHelper;
import com.dreamcode.caloriestracker.modelclass.ProfileDetails;

public class ProfileDAOimpl implements ProfileDAO {
	private static ProfileDAOimpl profileDAOimpl = null;
	private DbHelper dbHelper = null;
	
	public static ProfileDAOimpl getInstance(){
		if(profileDAOimpl==null){
			profileDAOimpl = new ProfileDAOimpl();
		}
		return profileDAOimpl;
	}
	
	@Override
	public void createProfile(Context ctx, ProfileDetails profileDetails) {
		dbHelper = DbHelper.getInstance(ctx.getApplicationContext());
		ContentValues contentValues = new ContentValues();
		contentValues.put("label", profileDetails.getProfileLabel());
		contentValues.put("age", profileDetails.getAge());
		contentValues.put("sex", profileDetails.getSex());
		contentValues.put("weight", profileDetails.getProfileWeight());
		contentValues.put("height", profileDetails.getProfileHeight());
		contentValues.put("waist", profileDetails.getProfileWaist());
		contentValues.put("paramwe", profileDetails.getWeParam());
		contentValues.put("paramhe", profileDetails.getHeParam());
		contentValues.put("paramwa", profileDetails.getWaParam());
		dbHelper.getDb().insert(dbHelper.DB_TABLE_PROFILE, null, contentValues);		
	}

	@Override
	public ProfileDetails getProfileDetails(Context ctx, int id) {
		ProfileDetails profileDetails = null;
		dbHelper = DbHelper.getInstance(ctx.getApplicationContext());
		Cursor c = dbHelper.getDb().query(dbHelper.DB_TABLE_PROFILE, null, null, null, null, null, null);
		if(c!=null && c.moveToNext()){			
			profileDetails = new ProfileDetails();
			profileDetails.setId(c.getInt(c.getColumnIndex("_id")));
			profileDetails.setProfileLabel(c.getString(c.getColumnIndex("label")));
			profileDetails.setAge(c.getString(c.getColumnIndex("age")));
			profileDetails.setSex(c.getString(c.getColumnIndex("sex")));
			profileDetails.setProfileWeight(c.getString(c.getColumnIndex("weight")));
			profileDetails.setProfileHeight(c.getString(c.getColumnIndex("height")));
			profileDetails.setProfileWaist(c.getString(c.getColumnIndex("waist")));
			profileDetails.setWeParam(c.getString(c.getColumnIndex("paramwe")));
			profileDetails.setHeParam(c.getString(c.getColumnIndex("paramhe")));
			profileDetails.setWaParam(c.getString(c.getColumnIndex("paramwa")));
		}		
		return profileDetails;
	}

	@Override
	public void deleteProfileAccount(Context ctx, int id) {
		dbHelper = DbHelper.getInstance(ctx.getApplicationContext());
		dbHelper.getDb().delete(dbHelper.DB_TABLE_PROFILE, "_id="+id, null);		
	}

    @Override
    public void updateProfileAccount(Context ctx, ProfileDetails profileDetails) {
        dbHelper = DbHelper.getInstance(ctx.getApplicationContext());
        ContentValues contentValues = new ContentValues();
        contentValues.put("label", profileDetails.getProfileLabel());
        contentValues.put("age", profileDetails.getAge());
        contentValues.put("sex", profileDetails.getSex());
        contentValues.put("weight", profileDetails.getProfileWeight());
        contentValues.put("height", profileDetails.getProfileHeight());
        contentValues.put("waist", profileDetails.getProfileWaist());
        contentValues.put("paramwe", profileDetails.getWeParam());
		contentValues.put("paramhe", profileDetails.getHeParam());
		contentValues.put("paramwa", profileDetails.getWaParam());
        dbHelper.getDb().update(dbHelper.DB_TABLE_PROFILE, contentValues,
                "_id=" + profileDetails.getId(), null);              
    }
	
	

}
